/**
 * JAVACC DEMO 1.0
 */
package com.apache.rpc.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.rpc.manager.InterfaceRegisterManager;
import com.apache.tools.ClassToolsUtil;
import com.apache.tools.StrUtil;

/**
 * description:  接口注册action
 *
 * @author Administrator 创建时间：2016-11-30
 */
public class ProtocolActionPluginImpl implements PluginConnector {

    private InterfaceRegisterManager interfaceRegisterManager;

    private IDao interfaceRegisterDao;

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see PluginConnector#execute(ParamsVo)
     */
    public Object execute(ParamsVo param) throws Exception {
        String method = String.valueOf(param.getParams("method"));
        ResultEntity entity = new ResultEntity();
        entity.setResult("true");
        if ("loadCache".equalsIgnoreCase(method)) {
            loadCache(entity, param);
        } else if ("list".equalsIgnoreCase(method)) {
            list(entity, param);
        } else if ("save".equalsIgnoreCase(method)) {
            save(entity, param);
        } else if ("info".equalsIgnoreCase(method)) {
            info(entity, param);
        }
        param = null;
        return entity;
    }

    private void info(ResultEntity entity, ParamsVo param) {
        String registerId = String.valueOf(param.getParams("registerId"));
        MethodParam pm = new MethodParam("ById", "", "", InterfaceRegister.class.getName());
        pm.setParams("registerId", registerId);
        Object obj = interfaceRegisterDao.selectSingle(pm);
        entity.setEntity(obj);
        entity.setMessage("查询成功");
    }

    private void loadCache(ResultEntity entity, ParamsVo param) {
        ParamsVo<InterfaceRegister> vo = new ParamsVo<InterfaceRegister>();
        vo.setMethodKey("initCache");
        interfaceRegisterManager.execute(vo);
        entity.setEntity("true");
        entity.setMessage("操作成功！");
    }

    private void list(ResultEntity entity, ParamsVo param) {
        ParamsVo<InterfaceRegister> vo = new ParamsVo<InterfaceRegister>();
        String beanId = String.valueOf(param.getParams("beanId"));
        String beanName = String.valueOf(param.getParams("beanName"));
        String flag = String.valueOf(param.getParams("flag"));
        String address = String.valueOf(param.getParams("address"));
        String port = String.valueOf(param.getParams("port"));
        MethodParam pm = new MethodParam("ByProperty", "", "", InterfaceRegister.class.getName());
        if (Validator.isNotNull(beanId)) {
            pm.setParams("beanId", beanId);
        }
        if (Validator.isNotNull(beanName)) {
            pm.setParams("beanName", beanName);
        }
        if (Validator.isNotNull(flag)) {
            pm.setParams("flag", flag);
        }
        if (Validator.isNotNull(port)) {
            pm.setParams("port", port);
        }
        if (Validator.isNotNull(address)) {
            pm.setParams("address", address);
        }
        String rows = StrUtil.doNull(String.valueOf(param.getParams("pageSize")),
                String.valueOf(param.getParams("rows")));
        String page = StrUtil.doNull(String.valueOf(param.getParams("pageIndex")),
                String.valueOf(param.getParams("page")));
        pm.setPageSize(Integer.valueOf(Validator.getDefaultStr(rows, "20")));
        pm.setPageIndex(Integer.valueOf(Validator.getDefaultStr(page, "1")));

        Page pageList = interfaceRegisterDao.pageSelect(pm);
        entity.setEntity(pageList);
        entity.setMessage("查询成功");
    }

    private void save(ResultEntity entity, ParamsVo vo) {
        ParamsVo<InterfaceRegister> param = new ParamsVo<InterfaceRegister>();
        InterfaceRegister item = new InterfaceRegister();
        item = (InterfaceRegister) ClassToolsUtil.getInstance().convert2Bean(vo.getParams(), item);
        param.setObj(item);
        if (Validator.isNull(item.getRegisterId())) {
            param.setMethodKey("checkInfo");
            long count = Long.parseLong(String.valueOf(interfaceRegisterManager.execute(param)));
            if (count > 0) {
                entity.setMessage("[" + item.getBeanId() + "]已存在不能重得添加");
                return;
            }
            item.setTimestampCreate(System.currentTimeMillis());
            item.setUserIdCreate("");
            String id = interfaceRegisterManager.saveInfo(param);
            if (Validator.isNull(id)) {
                entity.setMessage("保存失败");
            } else {
                entity.setEntity(id);
                entity.setMessage("保存成功");
            }
        } else {
            boolean mark = interfaceRegisterManager.editInfo(param);
            if (mark) {
                entity.setEntity(mark);
                entity.setMessage("修改成功");
            } else {
                entity.setMessage("修改失败");
            }
        }
    }

    public void setInterfaceRegisterManager(InterfaceRegisterManager interfaceRegisterManager) {
        this.interfaceRegisterManager = interfaceRegisterManager;
    }

    public void setInterfaceRegisterDao(IDao interfaceRegisterDao) {
        this.interfaceRegisterDao = interfaceRegisterDao;
    }

}
